package com.company.Thread.Concurrent;



public class sync {
    int n = 0;
//    Lock l = new ReentrantLock();

    // AB两个进程想要的效果
    // A->B->A->B->........
    public static void main(String[] args) {
        sync s = new sync();

        new Thread(()->{
            for(int i=0; i<10; i++) {
                s.increase1();
            }
        }, "A").start();

        new Thread(()->{
            for(int i=0; i<10; i++) {
                s.increase1();
            }
        }, "B").start();
    }


    public synchronized void increase1(){
        n++;
        System.out.println(Thread.currentThread().getName() + ": " + n);
    }

    public void increase2(){
        synchronized (this){
            n++;
            System.out.println(Thread.currentThread().getName() + ": " + n);
        }
    }
}
